function popup(url, width, height) {
    var left=screen.width-width;
    var top=screen.height-height;
    var pWin=open( url, 'win', 'width='+width+',height='+height+',left='+left+',top='+top );
    if( pWin.screenX != null ) {
        pWin.screenX=left;
        pWin.screenY=top;
    }
    return pWin;
}
  
$(function(){
        $('.feedback_detail .detail_close, .feedback_like .detail_close, .feedback_dislike .detail_close').live('click', function(){
                $(this).parent().parent().children('.detail.coupon_detail').show();
                $(this).parent().hide();
        });
        $('.share_detail .detail_close, .comment_detail .detail_close').live('click', function(){
                $(this).parent().hide();
        });

        $('.commentLink').click(function(){
                $(this).parent().parent().parent().parent().children('.share_detail').hide();
                $(this).parent().parent().parent().parent().children('.comment_detail').toggle();

        });
        $('.pseudoLink').click(function(){
                var couponid = $(this).attr('couponid');
                var url = $(this).attr('data-url');
                $('#share_social_'+couponid).html('<fb:like href="'+url+'" send="false" layout="button_count" width="450" show_faces="false"></fb:like>');                                     
                FB.XFBML.parse(); 
                
                $(this).parent().parent().parent().children('.comment_detail').hide();
                $(this).parent().parent().parent().children('.share_detail').toggle();
        });

        $('.vote_like, .vote_dislike').live('click', function(){
                var coupon_id = $(this).parent().attr('coupon_id');
                var li = $(this).parent().parent().parent();

                if(customer_logined == 0){
                    li.children('.coupon_detail').hide();
                    li.children('.offer_detail.login').show();
                    return false;
                } 

                // update status button && content area
                if($(this).hasClass('vote_like'))
                    {
                    li.children('.feedback_dislike').hide();
                    li.removeClass('voted_down');
                    if(li.hasClass('voted_up')){
                        li.removeClass('voted_up');
                        li.children('.coupon_detail').show();
                        li.children('.feedback_like').hide();
                    }else{
                        li.addClass('voted_up'); 
                        li.children('.coupon_detail').hide();
                        li.children('.feedback_like').show();
                        li.children('.comment_detail').show();
                    }


                }
                else if($(this).hasClass('vote_dislike'))
                    {
                    li.children('.feedback_like').hide();
                    li.removeClass('voted_up');
                    if(li.hasClass('voted_down')){
                        li.removeClass('voted_down');
                        li.children('.coupon_detail').show();
                        li.children('.feedback_dislike').hide();
                    }else{
                        li.addClass('voted_down'); 
                        li.children('.coupon_detail').hide();
                        li.children('.feedback_dislike').show();
                    }

                };

                $.post('/coupon/vote', {coupon_id: coupon_id, vote: $(this).attr('ref')}, function(data){
                        //             $('.vote-count').text(data.vote_count);   
                        //             $('.like-count').text(data.like_count);   
                        //             $('.dislike-count').text(data.dislike_count);   
                        //             $('.like-bar').css('width', data.like_count_percent+'%');   
                        //             $('.dislike-bar').css('width', data.dislike_count_percent+'%');   
                    }, 'json');
        }); 


        $('.view_coupon').click(function(e){
                e.preventDefault();
                var url_tab = $(this).attr('href');
                var url_pop = $(this).attr('ref');

                var win_tab =  window.open(url_tab, $.browser.msie ? '_self' : '_blank');
                win_tab.focus();


                var width = 500;
                var height = 800;
                var left=screen.width-width;
                var top=screen.height-height;
                var win_pop=window.open(url_pop, '_blank', 'width='+width+',height='+height+',left='+left+',top='+top+', directories=0,menubar=0,');
                if( win_pop.screenX != null ) {
                    win_pop.screenX=left;
                    win_pop.screenY=top;
                }
                win_pop.focus();
        });

        $('#store_filter li').click(function(e){
                e.preventDefault();
                $('#ajax-loading').fadeIn('fast');
                $('#store_filter li').each(function(){
                        $(this).removeClass('active');
                });
                $(this).addClass('active');
                
                var id = $(this).attr('id');
                
                if(id == 'filter_all'){
                    $('ul.offer_list li.coupon').show();    
                }else{
                    
                    $('ul.offer_list li.coupon').hide();
                    
                    if(id == 'filter_sales')            $('ul.offer_list li.coupon.sales').show();
                    else if(id == 'filter_codes')       $('ul.offer_list li.coupon.codes').show();   
                    else if(id == 'filter_freeship')    $('ul.offer_list li.coupon.freeship').show();   
                }
                $('#ajax-loading').fadeOut('fast');
        });
        
        
        // tell us
        $('button.tell_us').click(function(){
            var saving = $(this).parent();
            var coupon_id = saving.attr('coupon_id');
            var dollars = $.trim($(this).parent().find('input.savings_dollars').val());
            var cents = $.trim($(this).parent().find('input.savings_cents').val());
            var purchase = $.trim($(this).parent().find('input.savings_purchased').val());
            if(dollars == '' || (dollars == 0 && cents == 0)){
                alert('You must enter the amount of dollars you have saved.');
                $(this).parent().find('input.savings_dollars').focus();
                return false;   
            }
            
            $('#ajax-loading').fadeIn();
            $.post('/coupon/vote', {coupon_id : coupon_id, dollars : dollars, cents : cents, purchase : purchase}, function(data){
                saving.html('<h3 class="success_note">Nice one! <span>Thanks for sharing how much you saved.</span></h3>');
                $('#ajax-loading').fadeOut();    
            })
        });
        
        


        

});

        FB.Event.subscribe('edge.create',function(response) {$.post('/coupon/like', {response:response});});
        FB.Event.subscribe('edge.remove',function(response) {$.post('/coupon/unlike', {response:response});});
        FB.Event.subscribe('comment.create',function(response) {$.post('/coupon/comment', {response:response});});
        FB.Event.subscribe('comment.remove',function(response) {$.post('/coupon/uncomment', {response:response});});